﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SnmpForm.APConfig
{
    public partial class FrmSnmpTrust : frmApConfig
    {
        public FrmSnmpTrust(BusinessWrapper.Ap ap)
            : base(ap)
        {
            InitializeComponent();
        }

        private void FrmSnmpTrust_Load(object sender, EventArgs e)
        {
            ucSnmpTrust1.initWith(m_ap.GetInfo());
        }

        public override bool Update(bool bView = true)
        {
            bool bSuccess = true;
            DbWrapper.DbDataTypes.Ap info =
                m_ap.GetInfo();
            if (bView)
            {
                ucSnmpTrust1.Update();
            }
            else
            {
                string strErr = "";
                if (ucSnmpTrust1.CheckInput(ref strErr))
                {
                    ucSnmpTrust1.Update(false);
                }
                else
                {
                    bSuccess = false;
                }
            }
            return bSuccess;
        }

        public override bool SyncToApCommit()
        {
            DbWrapper.DbDataTypes.Ap info =
                ucSnmpTrust1.Get();
            return m_ap.SetSecurePasswd(info.snmp_password);
        }

        public override bool SaveToDb()
        {
            Update(false);
            DbWrapper.DbDataTypes.Ap info = m_ap.GetInfo();
            info.snmp_password = ucSnmpTrust1.Get().snmp_password;
            bool bSuccess =
                BusinessWrapper.User.Instance.ModifyAp(info);
            if (bSuccess)
            {
                MessageBox.Show("保存成功");
                Close();
            }
            else
            {
                MessageBox.Show("保存失败");
            }
            return bSuccess;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (Update(false))
            {
                DialogResult result = MessageBox.Show("修改Ap托管密码，AP会自动重启\n是否继续",
                    "告警",
                    MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    if (SyncToAp())
                    {
                        if (SaveToDb())
                        {
                            Close();
                        }
                    }
                    else
                    {
                        MessageBox.Show("同步错误");
                    }
                }
            }
        }
    }
}
